package com.cyandpos.pad.offline.model;

import android.database.Cursor;
import android.util.Log;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.posdata.ClassNewspaperAndDaily;
import com.cyandpos.pad.posdata.ReportGroup;
import com.cyandpos.pad.posdata.ReportPay;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.Map;


public class OfflineReport {


    public static void report(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback) {
        ClassNewspaperAndDaily newspaperAndDaily = new ClassNewspaperAndDaily();
        getgroupArrayList(sqldb, params,newspaperAndDaily);
        newspaperAndDaily.setReportPayList(getreportPaysArrayList(sqldb, params,newspaperAndDaily)); // Report_Payments
        newspaperAndDaily.setCount_sale(newspaperAndDaily.getReportPayList().size());
        callback.onSuccess(CursorUtil.getOfflineBean(newspaperAndDaily));

    }
    public static ClassNewspaperAndDaily getgroupArrayList(Sqldb sqldb, Map<String, Object> params, ClassNewspaperAndDaily newspaperAndDaily) {
        ArrayList<ReportGroup> groupArrayList = new ArrayList<>();
        String sq = "";
        boolean bfind;

        ArrayList<ReportGroup>  ReportGuzuList = null;

        int  total_sale = 0;
        int  count_sale = 0;
        int  total_back = 0;
        int  count_back = 0;

        int total_sale_DJD = 0;
        int count_sale_DJD = 0;
        int total_back_DJD = 0;
        int count_back_DJD = 0;

        int total_sale_PreSale = 0;
        int count_sale_PreSale = 0;
        int total_back_PreSale = 0;
        int count_back_PreSale = 0;

        try {
            String sql1 = "select B.DDGN,A.DEPTID,C.BMDM,C.DEPT_NAME,B.JYLX,A.XSJE-A.ZKJE as XSJE,B.DJDSTATUS from POS_ORDER B,ORDER_ITEM A,BM C " +
                    "where A.ORDERNO=B.ORDERNO and A.DEPTID=C.DEPTID and B.FDBH=" +"'"+ params.get("storeCode") +"'"+ " and B.STATUS=1  and B.SKTNO=" +"'"+ params.get("sktno")+"'";

            if ("1".equals(params.get("bj") + "")) {
             //   sq = sql1 + " and B.JZRQ  between '2021-11-16 00:00:00' and '2021-11-16 23:59:59'";
                sq = sql1 + " and B.JZRQ  between '"+params.get("jzrq")+" 00:00:00' and '"+params.get("jzrq")+" 23:59:59'";
            }

            if ((int) params.get("personId")>0){
                sq = sq+" and B.SKY=" + "'"+params.get("personId")+"'";
            }
            Cursor cursor = sqldb.find(sq);
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    ReportGroup item = new ReportGroup();
                    item.setDeptId(CursorUtil.getInt(cursor, "DEPTID"));
                    item.setDeptName(CursorUtil.getString(cursor, "DEPT_NAME"));
                    item.setDeptCode(CursorUtil.getString(cursor, "BMDM"));
                    item.setOrderFunction(CursorUtil.getInt(cursor, "DDGN"));
                    item.setAmount(CursorUtil.getString(cursor, "XSJE"));
                    item.setDdlx(CursorUtil.getInt(cursor, "JYLX"));
                    groupArrayList.add(item);
                }
                ReportGuzuList = new ArrayList<ReportGroup>();

                for (int i = 0; i < groupArrayList.size(); i++)
                {
                    if (groupArrayList.get(i).getDdlx() == 0)
                    {
                        if (groupArrayList.get(i).getDjdStatus() != 1)
                        {
                            total_sale = total_sale + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());
                        }
                        if (groupArrayList.get(i).getOrderFunction() == 1)
                        {
                            total_sale_DJD = total_sale_DJD + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());
                        }
                        if (groupArrayList.get(i).getOrderFunction() == 4)
                        {
                            total_sale_PreSale = total_sale_PreSale + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());
                        }
                    }
                    else if (groupArrayList.get(i).getDdlx() == 1)
                    {
                        // count_back = count_back + 1;
                        total_back = total_back + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());

                        if (groupArrayList.get(i).getOrderFunction()== 1)
                        {
                            total_back_DJD = total_back_DJD + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());
                        }
                        if (groupArrayList.get(i).getOrderFunction() == 4)
                        {
                            total_back_PreSale = total_back_PreSale + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount());
                        }
                    }
                    if (i == 0)
                    {
                        ReportGuzuList.add(groupArrayList.get(i));

                        continue;
                    }
                    bfind = false;
                    for (int  j = 0; j < ReportGuzuList.size(); j++)
                    {

                        if (ReportGuzuList.get(j).getDeptId() == groupArrayList.get(i).getDeptId())  //getMoneyStr
                        {
                             //
                            ReportGuzuList.get(j).setAmount(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(ReportGuzuList.get(j).getAmount()) + MoneyUtils.getMoneyInt(groupArrayList.get(i).getAmount())));

                            bfind = true;
                        }
                    }
                    if (!bfind)
                    {
                        ReportGuzuList.add(groupArrayList.get(i));
                    }
                }
                cursor.close();
                newspaperAndDaily.setTotal_sale((total_sale/100)+"");
                newspaperAndDaily.setTotal_back((total_back/100)+"");
                newspaperAndDaily.setReportGroupList(groupArrayList);

            }else {
                  // 没数据呢
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String sq2 = "";
            String sql1 = "select  JYLX,DDGN  from POS_ORDER  " +
                    "where FDBH="+ "'"+params.get("storeCode") + "'"+" AND STATUS=1 and SKTNO=" +"'"+ params.get("sktno")+"'";
            if ("1".equals(params.get("bj") + "")) {
                  sq2 = sql1 + "and JZRQ  between '"+params.get("jzrq")+" 00:00:00' and '"+params.get("jzrq")+" 23:59:59'";

            }
            if ((int) params.get("personId")>0){
                sq2 = sq2+"and SKY=" + "'"+params.get("personId")+"'";
            }

                   Cursor cursor = sqldb.find(sq2);
                if (cursor != null && cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                    if (CursorUtil.getInt(cursor, "JYLX")==0){ //这儿报错
                        count_sale = count_sale + 1;
                        if (CursorUtil.getInt(cursor, "DDGN")==1){
                            count_sale_DJD = count_sale_DJD + 1;
                        }
                        if (CursorUtil.getInt(cursor, "DDGN") == 4)
                        {
                            count_sale_PreSale = count_sale_PreSale + 1;
                        }
                    }else if (CursorUtil.getInt(cursor, "JYLX")==1){
                        count_back = count_back + 1;
                        if (CursorUtil.getInt(cursor, "DDGN")==1){  //订金单
                            count_back_DJD = count_back_DJD + 1;
                        }
                        if (CursorUtil.getInt(cursor, "DDGN") == 4)  //预售单
                        {
                            count_back_PreSale = count_back_PreSale + 1;
                        }
                    }
                }
                newspaperAndDaily.setCount_sale(count_sale);
                newspaperAndDaily.setCount_back(count_back);
                cursor.close();
            }else {
                //没查询到
            }
        } catch (Exception e) {
            Log.i("TAG----",e.toString());
            e.printStackTrace();
        }

        //   return groupArrayList;
        return newspaperAndDaily;

    }

                      //收款方式数据
    public static ArrayList<ReportPay> getreportPaysArrayList(Sqldb sqldb, Map<String, Object> params, ClassNewspaperAndDaily newspaperAndDaily) {
        ArrayList<ReportPay> reportPays = new ArrayList<>();
        String sq = "";
        try {
            String sql1 = " select A.SKFS,SKJE,YYJE,C.TYPE,C.NAME  from POS_ORDER B, ORDER_PAY A,SKFS C " +
                    "where A.ORDERNO=B.ORDERNO and A.SKFS = C.CODE and B.FDBH=" + "'"+params.get("storeCode") + "'"+" AND B.STATUS=1 and B.SKTNO=" + "'"+params.get("sktno")+"'";

            if ("1".equals(params.get("bj") + "")) {
                sq = sql1 + " and B.JZRQ  between '"+params.get("jzrq")+" 00:00:00' and '"+params.get("jzrq")+" 23:59:59'";

            }
            if ((int) params.get("personId")>0){

                sq = sq+"and B.SKY="+ "'"+params.get("personId")+"'";
            }

                //sq=sq+" GROUP BY A.SKFS,C.TYPE,C.NAME";

            Cursor cursor = sqldb.find(sq);
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    ReportPay item = new ReportPay();
                    item.setName(CursorUtil.getString(cursor, "NAME"));
                    item.setAmount(CursorUtil.getString(cursor, "SKJE"));
                    if (CursorUtil.getString(cursor, "YYJE").equals("")|| CursorUtil.getString(cursor, "YYJE")==null){
                        item.setAmount_yy("0");
                    }else {
                        item.setAmount_yy(CursorUtil.getString(cursor, "YYJE"));
                    }
                    item.setCode(CursorUtil.getString(cursor, "SKFS"));
                    item.setType(CursorUtil.getInt(cursor, "TYPE"));
                    reportPays.add(item);
                }
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return reportPays;

    }

}